using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;


public interface IEventInfo { }

public class EventInfo<T> : IEventInfo
{
    public UnityAction<T> actions;

    public EventInfo(UnityAction<T> action)
    {
        actions += action;
    }
}

public class EventInfo : IEventInfo
{
    public UnityAction actions;

    public EventInfo(UnityAction action)
    {
        actions += action;
    }
}

/// <summary>
/// 事件中心
/// </summary>
public class ListenerMgr : Singleton<ListenerMgr>
{
    private Dictionary<string, IEventInfo> triggers = new Dictionary<string, IEventInfo>();

    /// <summary>
    /// 添加事件监听
    /// </summary>
    /// <param name="name">事件名称</param>
    /// <param name="action">事件触发时的回调</param>
    public void AddTrigger<T>(string name, UnityAction<T> action)
    {
        if (!triggers.ContainsKey(name))
        {
            // Debug.LogFormat("[ListenerMgr][AddTrigger] name:{0}", name);
            triggers.Add(name, new EventInfo<T>(action));
            return;
        }
        // Debug.LogFormat("[ListenerMgr][AddTrigger] name:{0}", name);
        (triggers[name] as EventInfo<T>).actions += action;
    }

    /// <summary>
    /// 添加事件监听
    /// </summary>
    /// <param name="name">事件名称</param>
    /// <param name="action">事件触发时的回调</param>
    public void AddTrigger(string name, UnityAction action)
    {
        if (!triggers.ContainsKey(name))
        {
            // Debug.LogFormat("[ListenerMgr][AddTrigger] name:{0}", name);
            triggers.Add(name, new EventInfo(action));
            return;
        }
        // Debug.LogFormat("[ListenerMgr][AddTrigger] name:{0}", name);
        (triggers[name] as EventInfo).actions += action;
    }

    /// <summary>
    /// 移除指定监听者对指定事件的监听
    /// </summary>
    /// <param name="listener">监听者标识对象</param>
    /// <param name="name">事件名称</param>
    public void RemoveTrigger<T>(string name, UnityAction<T> action)
    {
        if (!triggers.ContainsKey(name))
            return;
        (triggers[name] as EventInfo<T>).actions -= action;
        // Debug.LogFormat("[ListenerMgr][RemoveTrigger] name:{0}", name);
    }

    /// <summary>
    /// 移除指定监听者对指定事件的监听
    /// </summary>
    /// <param name="listener">监听者标识对象</param>
    /// <param name="name">事件名称</param>
    public void RemoveTrigger(string name, UnityAction action)
    {
        if (!triggers.ContainsKey(name))
            return;
        (triggers[name] as EventInfo).actions -= action;
        // Debug.LogFormat("[ListenerMgr][RemoveTrigger] name:{0}", name);
    }

    /// <summary>
    /// 触发指定事件
    /// </summary>
    /// <param name="name">要触发的事件名称</param>
    public void Trigger<T>(string name, T param)
    {
        if (!triggers.ContainsKey(name))
            return;
        // Debug.LogFormat("[ListenerMgr][Trigger] name:{0}", name);
        UnityAction<T> actions = (triggers[name] as EventInfo<T>).actions;
        if (actions != null)
            (triggers[name] as EventInfo<T>).actions.Invoke(param);
    }

    /// <summary>
    /// 触发指定事件
    /// </summary>
    /// <param name="name">要触发的事件名称</param>
    public void Trigger(string name)
    {
        if (!triggers.ContainsKey(name))
            return;
        // Debug.LogFormat("[ListenerMgr][Trigger] name:{0}", name);
        UnityAction actions = (triggers[name] as EventInfo).actions;
        if (actions != null)
            (triggers[name] as EventInfo).actions.Invoke();
    }

    /// <summary>
    /// 清理所有事件监听
    /// 场景切换时调用
    /// </summary>
    public void ClearAllTrigger()
    {
        triggers.Clear();
    }
}
